<template>
  <div class="employeesMng-box">
    <!-- <div class="tips-box">
      <h1>员工管理</h1>
      <span>管理您店铺内所有员工的账号、权限等。</span>
    </div> -->
    <div class="content-box w-card">
      <div class="title-box">
        <span>员工列表</span>
        <el-button style="margin-left:auto;" type="primary" icon="Plus" @click="addUser">创建员工</el-button>
      </div>
      <div class="search-box">
        <div class="form-item">
          <span>员工姓名：</span>
          <el-input v-model="searchForm.name" clearable placeholder="请输入员工姓名" @keypress.enter="searchHandle" />
        </div>
        <div class="form-item">
          <span>状态：</span>
          <el-select style="width:100%" v-model="searchForm.state">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
        <el-button type="primary" @click="searchHandle">搜索</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="name" label="员工姓名" align="center">
            <template v-slot="scope">
              {{ scope.row.name || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="联系电话" align="center">
            <template v-slot="scope">
              {{ scope.row.phone || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="roleName" label="岗位" align="center">
            <template v-slot="scope">
              {{ scope.row.roleName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template v-slot="scope">
              <span :class="'state'+scope.row.state"></span>
              {{ scope.row.state == 0 ? '停用' : '启用' }}
            </template>
          </el-table-column>
          <el-table-column prop="Operations" label="操作" align="center" width="160">
            <template v-slot="scope">
              <div class="table-handle-box">
                <el-link style="font-size:14px" type="primary" @click="lookHanlde(1,scope.row)">编辑</el-link>
                <el-link v-if="scope.row.state == 0" style="font-size:14px" type="primary"
                  @click="lookHanlde(2,scope.row)">启用</el-link>
                <el-link v-if="scope.row.state == 1" style="font-size:14px" type="danger"
                  @click="lookHanlde(3,scope.row)">停用</el-link>
                <el-link style="font-size:14px" type="danger" @click="lookHanlde(4,scope.row)">删除</el-link>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <confirm-pass ref="confirmPassRef" @verification="verification" />
  <edit-dialog ref="editDialogRef" @dialogClose="editDialogClose" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal } from '@/utils/utils'
import api from './api'
import confirmPass from '@/components/confirmPass.vue'
import editDialog from './components/editDialog.vue'
export default {
  components: {
    confirmPass,
    editDialog,
  },
  setup() {
    const confirmPassRef = ref(null)
    const editDialogRef = ref(null)

    const state = reactive({
      dealData: null,
      dealType: null,
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      searchForm: {
        state: null,
      },
      tableData: [],
      statusOptions: [
        {
          value: null,
          label: '全部',
        },
        {
          value: 0,
          label: '禁用',
        },
        {
          value: 1,
          label: '启用',
        },
      ],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
      }
      api.userSearch(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.records
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      //搜索
      state.page = 1
      getTableData()
    }

    function lookHanlde(index, data) {
      state.dealData = data
      state.dealType = index
      let title = ''
      let msg = ''
      switch (index) {
        case 1:
          //编辑
          editDialogRef.value.dialogShow(data)
          break
        case 2:
          //启用
          title = '员工启用提示'
          msg = '员工启用后，该员工账号可登录平台。'
          confirmPassRef.value.dialogShow(title, msg)
          break
        case 3:
          //停用
          title = '员工停用提示'
          msg = '员工停用后，该员工账号不可登录平台。'
          confirmPassRef.value.dialogShow(title, msg)
          break
        case 4:
          //删除
          title = '员工删除提示'
          msg = '是否确认删除该员工？'
          confirmPassRef.value.dialogShow(title, msg)
          break
      }
    }

    //密码校验通过
    function verification() {
      if (state.dealType == 1) {
      } else if (state.dealType == 2) {
        userStateChange(1)
      } else if (state.dealType == 3) {
        userStateChange(0)
      } else if (state.dealType == 4) {
        userDelete()
      }
    }

    function userStateChange(is) {
      const obj = {
        state: is,
        userId: state.dealData.userId,
      }
      api.userStateChange(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function userDelete() {
      const obj = {
        userId: state.dealData.userId,
      }
      api.userDelete(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addUser() {
      editDialogRef.value.dialogShow()
    }

    function editDialogClose(is) {
      if (is) {
        getTableData()
      }
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 290
      getTableData()
    })

    return {
      ...toRefs(state),
      confirmPassRef,
      editDialogRef,
      searchHandle,
      getDateTime,
      lookHanlde,
      verification,
      editDialogClose,
      addUser,
    }
  }
}
</script>

<style lang="scss" scoped>
.employeesMng-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column;
  .content-box {
    flex-grow: 1;
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      display: flex;
      align-items: center;
      padding-bottom: 20px;
      > span {
        font-size: 16px;
        font-weight: bold;
      }
    }
    .search-box {
      display: flex;
      align-items: center;
      gap: 20px;

      .form-item {
        width: 25%;
        max-width: 300px;
        display: flex;
        align-items: center;
        gap: 5px;

        > span {
          font-size: 14px;
          flex-shrink: 0;
        }
      }
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        margin-top: 20px;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
.state0 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ff3939;
}
.state1 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #03b615;
}
.table-handle-box {
  display: flex;
  gap: 5px;
  align-items: center;
  justify-content: center;
}
</style>